from django.shortcuts import render
from django.views import View

from apps.contents.models import ContentCategory
from apps.contents.utils import get_categories



class IndexView(View):
    """首页广告"""

    def get(self, request):
        """提供首页广告界面"""
        # 查询商品频道和分类
        categories = get_categories()

        # 广告数据
        contents = {}
        content_categories = ContentCategory.objects.all()
        for cat in content_categories:
            contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

        # 渲染模板的上下文
        context = {
            'categories': categories,
            'contents': contents,
        }
        return render(request, 'index.html', context)




# # # 导入库
# from fdfs_client.client import Fdfs_client
#
# # 创建实例对象
# client = Fdfs_client('utils/fastdfs/client.conf')
#
# # 上传图片
# client.upload_by_filename('/home/python/Desktop/cap.jpg')

# '''
# {'Remote file_id': 'group1/M00/00/00/wKgRPV0xiUiAdViwAABFt4lwIHM487.jpg',
# 'Local file name': '/home/python/Desktop/cap.jpg', 'Storage IP': '192.168.17.61',
# 'Group name': 'group1', 'Status': 'Upload successed.', 'Uploaded size': '17.00KB'}
#
# '''